﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace FPP.Models
{
    public class FppRepository
    {
        private readonly IFppEntities _context;

        public FppRepository(IFppEntities context)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            _context = context;
        }

        public void SaveChanges()
        {
            _context.SaveChanges();
        }

        public bool AddFamousPerson(FamousPerson fp)
        {
            var rtn = fp.Validate(ChangeAction.Insert);

            if (rtn) _context.FamousPersons.AddObject(fp);

            return rtn;
        }

        public List<FamousPerson> GetListOfFamousPersons()
        {
            return _context.FamousPersons.ToList();
        }
        public List<User> GetListOfUsers()
        {
            return _context.Users.ToList();
        }

        public bool AddUser(User newUser)
        {
            var rtn = newUser.Validate(ChangeAction.Insert);

            if (rtn) _context.Users.AddObject(newUser);

            return rtn;
        }

        public bool AddBachelorGame(BachelorGame bachelorGame)
        {
            var rtn = bachelorGame.Validate(ChangeAction.Insert);

            if (rtn) _context.BachelorGames.AddObject(bachelorGame);

            return rtn;
        }

        public BachelorPerson GetBachelorPerson(int bachelorGameId)
        {
            var rtn = _context.BachelorPersons.First(bp => (bp.BachelorGameId == bachelorGameId) && (bp.IsBachelor));
            return rtn;
        }

        public bool AddBachelorPerson(BachelorPerson bachelorPerson)
        {
            var rtn = bachelorPerson.Validate(ChangeAction.Insert);
            if (rtn) _context.BachelorPersons.AddObject(bachelorPerson);
            return rtn;
        }

        public IQueryable<BachelorPerson> GetBachelorContestants(int bachelorGameId)
        {
            var rtn = _context.BachelorPersons.Where(bp => (bp.BachelorGameId == bachelorGameId) && (!bp.IsBachelor));
            return rtn;
        }
    }

}